/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iodegree;

import iodegree.lib.MyTableModel;
import iodegree.lib.database.DBHandle;
import iodegree.lib.database.LegacyDB;
import iodegree.lib.pattern.Pattern;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Libra
 */
public class ChR2ClusterUI extends javax.swing.JFrame {

    /**
     * Creates new form ChR2Cluster
     */
    public ChR2ClusterUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        DataFileChooser = new javax.swing.JFileChooser();
        gluGABAGroup = new javax.swing.ButtonGroup();
        btnOpenDbDefault = new javax.swing.JButton();
        btnOpenDatabase = new javax.swing.JButton();
        lblOpenDatabase = new javax.swing.JLabel();
        btnQryActPatt = new javax.swing.JButton();
        rdoGABA = new javax.swing.JRadioButton();
        rdoGlu = new javax.swing.JRadioButton();
        rdoBoth1 = new javax.swing.JRadioButton();
        statsPane = new javax.swing.JScrollPane();
        statsTable = new javax.swing.JTable();
        txtMinPosI = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnOpenDbDefault.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnOpenDbDefault.setText("Default");
        btnOpenDbDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenDbDefaultActionPerformed(evt);
            }
        });

        btnOpenDatabase.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnOpenDatabase.setText("Open");
        btnOpenDatabase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenDatabaseActionPerformed(evt);
            }
        });

        lblOpenDatabase.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblOpenDatabase.setText("Open Database File :");

        btnQryActPatt.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnQryActPatt.setText("Query Pattern");
        btnQryActPatt.setEnabled(false);
        btnQryActPatt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQryActPattActionPerformed(evt);
            }
        });

        gluGABAGroup.add(rdoGABA);
        rdoGABA.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoGABA.setText("GABA");
        rdoGABA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoGABAActionPerformed(evt);
            }
        });

        gluGABAGroup.add(rdoGlu);
        rdoGlu.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoGlu.setText("Glu");
        rdoGlu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoGluActionPerformed(evt);
            }
        });

        gluGABAGroup.add(rdoBoth1);
        rdoBoth1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoBoth1.setSelected(true);
        rdoBoth1.setText("Both");
        rdoBoth1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoBoth1ActionPerformed(evt);
            }
        });

        statsTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        statsPane.setViewportView(statsTable);

        txtMinPosI.setText("15");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblOpenDatabase)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rdoBoth1)
                                .addGap(18, 18, 18)
                                .addComponent(rdoGlu)
                                .addGap(18, 18, 18)
                                .addComponent(rdoGABA)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnOpenDatabase)
                                .addGap(18, 18, 18)
                                .addComponent(btnOpenDbDefault)
                                .addGap(18, 18, 18)
                                .addComponent(btnQryActPatt)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                                .addComponent(txtMinPosI, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(statsPane, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(lblOpenDatabase))
                    .addComponent(btnOpenDatabase)
                    .addComponent(btnOpenDbDefault)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnQryActPatt)
                        .addComponent(txtMinPosI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(rdoGlu)
                    .addComponent(rdoGABA)
                    .addComponent(rdoBoth1))
                .addGap(40, 40, 40)
                .addComponent(statsPane, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOpenDbDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenDbDefaultActionPerformed
        pathToFile = "C:\\Users\\Libra\\Desktop\\ChR2A.accdb";
        File f = new File(pathToFile);
        if (f.exists()) {
//            System.out.println("File " + pathToFile + " opened.");
            btnQryActPatt.setEnabled(true);
        }
    }//GEN-LAST:event_btnOpenDbDefaultActionPerformed

    private void btnOpenDatabaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenDatabaseActionPerformed
        int returnVal = DataFileChooser.showOpenDialog(this);
        if (returnVal == DataFileChooser.APPROVE_OPTION) {
            pathToFile = DataFileChooser.getSelectedFile().getAbsolutePath();
//            System.out.println("File " + pathToFile + " opened.");
            btnQryActPatt.setEnabled(true);
        }
    }//GEN-LAST:event_btnOpenDatabaseActionPerformed

    private void btnQryActPattActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQryActPattActionPerformed
        getActualCluster();
//        btnGenRnd.setEnabled(true);
//        btnGenRndM1.setEnabled(true);
//        btnGenRndM2.setEnabled(true);
    }//GEN-LAST:event_btnQryActPattActionPerformed

    private void rdoGABAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoGABAActionPerformed
        type = 2;
//        btnGenRnd.setEnabled(false);
//        btnGenRndM1.setEnabled(false);
//        btnGenRndM2.setEnabled(false);
    }//GEN-LAST:event_rdoGABAActionPerformed

    private void rdoGluActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoGluActionPerformed
        type = 1;
//        btnGenRnd.setEnabled(false);
//        btnGenRndM1.setEnabled(false);
//        btnGenRndM2.setEnabled(false);
    }//GEN-LAST:event_rdoGluActionPerformed

    private void rdoBoth1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoBoth1ActionPerformed
        type = 0;
//        btnGenRnd.setEnabled(false);
//        btnGenRndM1.setEnabled(false);
//        btnGenRndM2.setEnabled(false);
    }//GEN-LAST:event_rdoBoth1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChR2ClusterUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChR2ClusterUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChR2ClusterUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChR2ClusterUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChR2ClusterUI().setVisible(true);
            }
        });
    }

    private void getActualCluster() {
        Pattern p = new Pattern();
        DBHandle connType = new DBHandle();
        int posi = Integer.parseInt(txtMinPosI.getText());
        ArrayList<Integer[]> triGrp = p.chr2Split(
                LegacyDB.chr2SizeNIdSplit(pathToFile, div), posi);
        int[][] connList = connType.connType(pathToFile, div);

        HashMap<Integer, Integer> map = new HashMap<>(connList.length * 2);
        for (int i = 0; i < connList.length; i++) {
            map.put((connList[i][0] << 12) + connList[i][1], connList[i][2]);
        }

        int[][] histo = p.chr2ClusterCount(triGrp, map, type);

//        System.err.print(Arrays.deepToString(histo));

        Integer[][] statData = new Integer[4][7];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 7; j++) {

                statData[i][j] = Integer.valueOf(histo[i][j]);
            }
            MyTableModel actTableM = new MyTableModel(statData);
            statsPane.setColumnHeaderView(null);
            statsTable.setModel(actTableM);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser DataFileChooser;
    private javax.swing.JButton btnOpenDatabase;
    private javax.swing.JButton btnOpenDbDefault;
    private javax.swing.JButton btnQryActPatt;
    private javax.swing.ButtonGroup gluGABAGroup;
    private javax.swing.JLabel lblOpenDatabase;
    private javax.swing.JRadioButton rdoBoth;
    private javax.swing.JRadioButton rdoBoth1;
    private javax.swing.JRadioButton rdoGABA;
    private javax.swing.JRadioButton rdoGlu;
    private javax.swing.JScrollPane statsPane;
    private transient javax.swing.JTable statsTable;
    private javax.swing.JTextField txtMinPosI;
    // End of variables declaration//GEN-END:variables
    String pathToFile;
    int type;
    int div = 0;
}
